//
//  HEOldOrgCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/26.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEOldOrgCell.h"
#import "HDRatingStarView.h"

@interface HEOldOrgCell ()

@property(nonatomic, strong) UILabel *titleLb;
@property(nonatomic, strong) UILabel *distanceLb;
@property(nonatomic, strong) UILabel *addressLb;
@property(nonatomic, strong) UILabel *priceLb;

@property(nonatomic, strong) UIImageView *logoView;
@property (nonatomic, strong) HDRatingStarView *startView;

@property (nonatomic, strong) UIButton *bedBtn;
@property (nonatomic, strong) UIButton *phoneBtn;

@end

@implementation HEOldOrgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
  
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    UIImageView *logoView = [UIImageView new];
    _logoView = logoView;
    logoView.contentMode = UIViewContentModeScaleAspectFill; 
    [self.contentView addSubview:logoView];
    
    _titleLb = [UILabel new];
    _titleLb.font = DYFont(16);
    _titleLb.text = @"-";
    _titleLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview:_titleLb];
    
    _distanceLb = [UILabel new];
    _distanceLb.font = DYFont(15);
    _distanceLb.text = @"0m";
    _distanceLb.textColor = CNavSubTitleColor;
    [self.contentView addSubview:_distanceLb];
    
    _addressLb = [UILabel new];
    _addressLb.font = DYFont(15);
    _addressLb.text = @"-";
    _addressLb.textColor = CNavSubTitleColor;
    _addressLb.numberOfLines = 0;
    [self.contentView addSubview:_addressLb];
    
    
    [_logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.width.mas_offset(90);
        make.height.mas_offset(70);
    }];
    [_logoView hg_setAllCornerWithCornerRadius:6];
    
        
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.equalTo(_logoView.mas_right).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    [_titleLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
    [_distanceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(_titleLb.mas_left);
    }];
    [_distanceLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    UIView *line = [UIView new];
    line.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:line];

    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(_distanceLb.mas_right).mas_offset(10);
        make.width.mas_offset(1);
        make.height.mas_offset(15);
    }];

    [_addressLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(line.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-15);
    }];
    
    
    // 星星评分
    _startView = [[HDRatingStarView alloc] init];
    _startView.renderColor = KHexColor(@"F67605");
    _startView.defaultColor = KHexColor(@"CDCDCD");
    _startView.allowTouchToSelectScore = NO;
    _startView.score = 0;
    _startView.starWidth = 15;
    _startView.starImage = IMAGE_NAMED(@"rating_sel");
    [self.contentView addSubview:_startView];
    
    [_startView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_addressLb.mas_bottom).mas_offset(5);
        make.left.equalTo(_titleLb.mas_left);
        make.width.mas_offset(100);
        make.height.mas_greaterThanOrEqualTo(15);
    }];
 
    _priceLb = [UILabel new];
    _priceLb.font = DYFont(15);
    _priceLb.text = @"-";
    _priceLb.textColor = CNavSubTitleColor;
    [self.contentView addSubview:_priceLb];
    [_priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_addressLb.mas_bottom).mas_offset(5);
        make.left.equalTo(_startView.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
  
    //  footer
    UIView *footerTopLine = [UIView new];
    footerTopLine.backgroundColor = CSplitLineColor;
    [self.contentView addSubview: footerTopLine];
    [footerTopLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_priceLb.mas_bottom).offset(5);
        make.left.equalTo(_titleLb.mas_left);
        make.right.equalTo(self.contentView).offset(-10);
        make.height.offset(1);
    }];
    
    UIButton *phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _phoneBtn = phoneBtn;
    phoneBtn.backgroundColor = CNavBgColor;
    phoneBtn.titleLabel.font = DYBlodFont(15);
    [phoneBtn setTitle:@"-" forState:UIControlStateNormal];
    [self.contentView addSubview: phoneBtn];
    [phoneBtn addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];

    UIButton *bedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _bedBtn = bedBtn;
    bedBtn.backgroundColor = CPriceColor;
    bedBtn.titleLabel.font = DYBlodFont(15);
    bedBtn.userInteractionEnabled = NO;
    [bedBtn setTitle:@"-" forState:UIControlStateNormal];
    [self.contentView addSubview: bedBtn];
     
    [phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footerTopLine.mas_bottom).offset(5);
        make.right.equalTo(self.contentView).offset(-10);
        make.height.offset(30);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    [bedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footerTopLine.mas_bottom).offset(5);
        make.right.equalTo(phoneBtn.mas_left).offset(-10);
        make.height.offset(30);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
}

- (void)callPhone {
    if ([DYTools isBlankString:_model.gddh]) {
        [Dialog toastBottom:@"暂无联系电话~"];
        return;
    }
    
    
    NSArray *phones = [_model.gddh componentsSeparatedByString:@"/"];
    KNActionSheet *sheet = [[KNActionSheet alloc] initWithTitle:nil cancelTitle:@"取消" titleArray:phones  actionSheetBlock:^(NSInteger buttonIndex) {
        if (buttonIndex >= 0) {
            [DYTools callPhone:phones[buttonIndex]];
        }
    }];
    [sheet show];
}

- (void)setModel:(HEOldPelOrgModel *)model {
    
    _model = model;
    
    [_logoView sd_setImageWithURL:[NSURL URLWithString: model.image_src] placeholderImage:IMAGE_NAMED(@"noImage")];
    _titleLb.text = model.company_name;
    _addressLb.text = model.xxdz;
    _distanceLb.text = model.Kdistace;
    _priceLb.text = model.KPrice;
 
    _startView.score = model.KScore;
    
    [_bedBtn setTitle:model.KBedStr forState:UIControlStateNormal];
    [_phoneBtn setTitle:model.KPhone forState:UIControlStateNormal];

    
    [_bedBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(model.KBedW);
    }];
    
    if ([DYTools isBlankString:_model.gddh]) {
        [_phoneBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(0);
        }];
    }else {
        [_phoneBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(model.KPhoneW);
        }];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
